package com.jsgt.greekfire.ui.market.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.fhx.core.base.BaseAdapter
import com.fhx.core.base.BaseViewHolder
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.DealsBean
import com.jsgt.greekfire.bean.Tick
import com.jsgt.greekfire.common.util.TimeUtil
import org.jetbrains.anko.textColor

class VolumeAdapter : BaseAdapter<DealsBean>() {
    override fun provideViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder? {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_volume, parent, false)
        return VH(view)
    }


    inner class VH(itemView: View) : BaseViewHolder(itemView) {
        var tvTime: TextView? = itemView.findViewById(R.id.tvTime)
        var tvDirect: TextView? = itemView.findViewById(R.id.tvDirect)
        var tvPrice: TextView? = itemView.findViewById(R.id.tvPrice)
        var tvNumber: TextView? = itemView.findViewById(R.id.tvNumber)

        fun bindData(bean: DealsBean?) {
            if (bean == null) {
                tvTime?.text = "--"
                tvDirect?.text = "--"
                tvPrice?.text = "--"
                tvNumber?.text = "--"
            } else {
                tvTime?.text = TimeUtil.time2String(bean.createdDate!!, "HH:mm:ss")
                if (bean.side == 2) {
                    tvDirect?.text = itemView.context.getString(R.string.text_sale)
                    tvDirect?.textColor =
                        ContextCompat.getColor(tvDirect?.context!!, R.color.typeRed)
                } else {
                    tvDirect?.text = itemView.context.getString(R.string.text_buy)
                    tvDirect?.textColor =
                        ContextCompat.getColor(tvDirect?.context!!, R.color.typeGreen)
                }
                tvPrice?.text = bean.price.toString()
                tvNumber?.text = bean.amount.toString()
            }
        }
    }

    override fun getItemCount(): Int {
        return 20
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val viewHolder = holder as VolumeAdapter.VH
        if (position < dataList().size) {
            viewHolder.bindData(dataList()[position])
        } else {
            viewHolder.bindData(null)
        }
    }
}